package models;

import java.util.Observable;

import sound.SoundManager;
import sound.SoundtrackPlayer;
import constants.MessageType;
import interfaces.Observer;
import logic.CollisionHandler;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.SweepGradient;
import android.util.Log;
import android.util.Pair;
import android.view.MotionEvent;
import android.widget.FrameLayout;

/**
 * 
 * @author Merete, Mirna, Esben, Knut og Stian
 * 
 */
public class Pig extends PhysicalObject implements java.util.Observer {

	/**
	 * TAG = the class name used for logging purposes score = the current score
	 */
	private static final String TAG = Pig.class.getSimpleName();
	private Score score;
	private Score localHigh;
	private int currentFrame = 0;
	private boolean isGameOver;
	private boolean flapping;
	private int direction = 1; // 0 left, 1 right

	private SoundtrackPlayer soundTrackPlayer;

	public Pig(Bitmap bitmap, double x, double y, int width, int height,
			Score score, Score localHigh, Context c, int frameCount) {

		super(bitmap, x, y, width, height, frameCount);
		soundTrackPlayer = new SoundtrackPlayer(c);
		motion.setHorizontalVelocity(0.2);
		
		// Initial score is 0.
		this.score = score;
		this.localHigh = localHigh;
		active = true;

		// Setup rects:
		srcRect = new Rect(0, 0, spriteWidth, spriteHeight);
		destRect = new Rect(0, 0, spriteWidth, spriteHeight);
	}

	public void resetSpeedAndPos() {
		x = 10;
		y = 240;
		motion.setHorizontalVelocity(0.2);
		motion.setVerticalVelocity(0);
		motion.setHorizontalAcceleration(0);
		motion.setVerticalAcceleration(0);
	}

	public void update() {
		/*
		 * Accelerate the Pig
		 */

		if (x >= 160) { // Sets the final position of the pig
			motion.setHorizontalVelocity(0);
		}

		motion.setVerticalVelocity(motion.getVerticalVelocity()
				+ motion.getVerticalAcceleration());

		/*
		 * Update position, if the pig has reached the top or bottom, stop the
		 * vertical movement
		 */
		x = motion.getHorizontalVelocity() + x;

		if (y <= 0) {
			y = 5;
			motion.setVerticalVelocity(0);
		} else if (y >= this.screenHeight - this.spriteHeight) {
			y = this.screenHeight - this.spriteHeight - 5;
			motion.setVerticalVelocity(0);
			motion.setVerticalAcceleration(0);

			/*
			 * Added to make the squishing sound when the pig is freeloading
			 * points.
			 */
			soundTrackPlayer.playScraping();
		} else {
			y = motion.getVerticalVelocity() + y;
		}

		/*
		 * Move the Pig to the next position
		 */
		rect.set((int) x, (int) y, spriteWidth + (int) x, spriteHeight
				+ (int) y);

		/*
		 * If the pig is flying upwards, update rect position to simulate
		 * flapping.
		 */

		if (flapping) {

			if (direction == 1) {
				currentFrame++;
				if (currentFrame > 2) {
					currentFrame = 1;
					direction = 0;
				}
			} else {
				currentFrame--;
				if (currentFrame < 0) {
					currentFrame = 1;
					direction = 1;
				}
			}

			// Update the rectangles

			srcRect.left = currentFrame * spriteWidth;
			srcRect.right = srcRect.left + spriteWidth;

		}

		destRect.set((int) x, (int) y, (int) x + spriteWidth, (int) y
				+ spriteHeight);

	}

	public void draw(Canvas canvas) {
		canvas.drawBitmap(bitmap, srcRect, destRect, null);
	}

	public void handleMotionEvent(MotionEvent event) {
		if (isGameOver)
			return;
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			motion.setVerticalAcceleration(-0.1);
			flapping = true;
			soundTrackPlayer.playFlapWings();
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			motion.setVerticalAcceleration(0.1);
			flapping = false;
		}
	}

	public void setGameOver(boolean b) {
		isGameOver = b;
	}
	
	public void update(Observable observable, Object data) {

		if (data instanceof Pair) {
			@SuppressWarnings({ "unchecked", "rawtypes" })
			Pair<PhysicalObject,MessageType> pair = ((Pair)data);
			PhysicalObject ob = pair.first;
			MessageType mt = pair.second;

			switch (mt) {
			case POISONMUSHROOMHIT:
				score.addPoints(-300);
				soundTrackPlayer.playMushroomCollision();
				break;
			case SPARKLEMUSHROOMHIT:
				score.addPoints(300);
				soundTrackPlayer.playMushroomCollision();
				break;
			case COLLISION:
				// reset score
				if (score.getScore() > localHigh.getScore()) {
					localHigh.setScore(score.getScore());
				}
				soundTrackPlayer.playGameOver();
				score.resetScore();

				break;
			}

		}
	}
}
